# $ make --version
# GNU Make 4.3
# Built for x86_64-pc-linux-gnu
# Copyright (C) 1988-2020 Free Software Foundation, Inc.
# License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
# This is free software: you are free to change and redistribute it.
# There is NO WARRANTY, to the extent permitted by law.
#bugs report : bug-make@gnu.org 

#shell
SHELL:=/bin/sh

#src fiels path
SRC_PATH:=$(shell pwd)
#src files
SRC_FILE:=$(wildcard *.cpp)
#src file count
SRC_FILE_COUNT:=$(words $(SRC_FILE))

#head files path
HEAD_FILES_PATH:=$(shell pwd)

OBJS:=$(SRC_FILE:.cpp=.o)
#current count
CURRENT_COUNT:=0

#cc
CC:=g++

#cflags
CFLAGS:= -I$(HEAD_FILES_PATH)/*.h

BIN_FILE:= testbin

define compile_file
$(1):$(2)
	$(eval CURRENT_COUNT := $(shell expr $(CURRENT_COUNT) + 1 ))
	$(eval PERCENTAGE := $(shell echo "scale=2; ($(CURRENT_COUNT)/$(SRC_FILE_COUNT))*100" | bc))
	@echo "[$(CURRENT_COUNT)/$(SRC_FILE_COUNT)] [$(PERCENTAGE)]% Compile at $(SRC_PATH)/$(2)"
	$(CC) $(CFLAGS) -c $(2) -o $(1)
endef

$(foreach src,$(SRC_FILE),$(eval $(call compile_file,$(subst .cpp,.o,$(src)),$(src))))
$(BIN_FILE):$(OBJS)
	@echo "+++++++++++++++++Linking all objects to $(BIN_FILE) Compile....at $(shell date)++++++++++++"
	$(CC) $(CFLAGS)  $(OBJS) -o $(BIN_FILE)

.PHONY:all
all:$(BIN_FILE)
	@echo "===============build successfully!==============="
.PHONY:clean
clean:
	sudo rm -rf $(BIN_FILE) $(OBJS)


